package cn.fintecher.pangolin.entity.mysql;

import cn.fintecher.pangolin.common.enums.BaseSelect;
import cn.fintecher.pangolin.common.enums.BaseStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 * @Auther: xiaqun
 * @Description: 角色实体
 * @Date: 10:36 2019/8/13
 */

@Data
@Entity
@Table(name = "role")
@ApiModel(value = "role", description = "角色实体")
public class Role extends BaseEntity {
    @ApiModelProperty(value = "角色名称")
    private String name;

    @ApiModelProperty(value = "角色状态")
    @Enumerated(EnumType.STRING)
    private BaseStatus status;

    @ApiModelProperty(value = "角色描述")
    private String description;

    @ApiModelProperty(value = "资源集合")
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "role_resource", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "resource_id"))
    private Set<Resource> resources;

    @ApiModelProperty(value = "创建人ID")
    private String operatorId;

    @ApiModelProperty(value = "创建时间")
    private Date operateTime;

    @ApiModelProperty(value = "更新人ID")
    private String updaterId;

    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

    @ApiModelProperty(value = "是否系统角色")
    @Enumerated(EnumType.STRING)
    private BaseSelect isSystem = BaseSelect.NO;

    /**
     * 处理创建人、更新人、创建时间、更新时间
     *
     * @param id   用户ID
     * @param date 时间
     * @param b    标识 true时设置创建人和创建时间
     */
    public void process(String id, Date date, boolean b) {
        if (b) {
            this.operatorId = id;
            this.operateTime = date;
        }
        this.updaterId = id;
        this.updateTime = date;
    }
}
